<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/5/5
 * Time: 16:02
 */

namespace app\model\weixin;


use app\common\lib\TencentCos;
use app\model\BaseModel;

class WeixinUserinfo extends BaseModel
{
    protected $table = 'video_weixin_userinfo';

    protected $primaryKey = 'weixin_user_id';

    public $timestamps = false;

    protected static $instance;

    public static function getInstance()
    {
        //        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return new self();
    }

    public function insertData($param)
    {
        $res = [];
        $imageLink = '';
        if(!empty($param['headimgurl'])){
            $res = TencentCos::getInstance()->uploadStringFile($param['headimgurl'],'weixin');
            $imageLink = 'https://'.$res['Location'];
        }
        $insert = [
            'openid' => $param['openid']??'',
            'nickname' => $param['nickname']??'',
            'sex' => $param['sex']??'0',
            'province' => $param['province']??'',
            'city' => $param['city']??'',
            'country' => $param['country']??'',
            'headimgurl' => $imageLink,
            'privilege' => isset($param['privilege'])?json_encode($param['privilege'],256):'',
            'unionid' => $param['unionid']??'',
            'session_key' => $param['session_key']??'',
            'created_at' => date('Y-m-d H:i:s'),
        ];

        return $this->insertGetId($insert);
    }
}